/**********************************************************************
 Copyright (C) 2010 Teppo Eeva

 This file is part of Arithmetic Game.
 For more information, see <http://code.google.com/p/arithmetic-game/>

 Arithmetic Game is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 Arithmetic Game is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with Arithmetic Game.  If not, see <http://www.gnu.org/licenses/>.
 **********************************************************************/

#include "highscoredialog.h"
#include "ui_highscoredialog.h"
#include <QStandardItemModel>
#include <QMessageBox>
#include <QDebug>
#include <QPushButton>
#include "highscoremanager.h"

HighScoreDialog::HighScoreDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::HighScoreDialog)
{
    ui->setupUi(this);
    connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(close()));
    connect(ui->buttonBox->button(QDialogButtonBox::Reset), SIGNAL(clicked()),
            this, SLOT(reset()));
    connect(ui->tabWidget, SIGNAL(currentChanged(int)), this, SLOT(setActiveTab(int)));
}

HighScoreDialog::~HighScoreDialog()
{
    delete ui;
}

void HighScoreDialog::exec(HighScoreManager& manager, ArithDiffLevel diffLevel)
{
    m_manager = &manager;
    ui->tabWidget->setCurrentIndex(diffLevel);
    setActiveTab(diffLevel);
    QDialog::exec();
}

void HighScoreDialog::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void HighScoreDialog::reset()
{
    qDebug() << "HighScoreDialog::reset";
        QMessageBox::StandardButton reply;
        reply = QMessageBox::question(this, tr("Reset high scores"),
                                        tr("Are you sure you want to reset high scores?"),
                                        QMessageBox::Yes | QMessageBox::Cancel);
        if (reply == QMessageBox::Yes)
        {
            emit resetHighScores();
            close();
        }
        else
        {
            qDebug() << "High score reset canceled";
        }

}

void HighScoreDialog::setActiveTab(int index)
{
    switch (index)
    {
    case Easy:
        m_model = m_manager->getHighScoreModel(Easy);
        ui->tableViewEasy->setModel(m_model);
        break;
    case Moderate:
    default:
        m_model = m_manager->getHighScoreModel(Moderate);
        ui->tableViewModerate->setModel(m_model);
        break;
    case Hard:
        m_model = m_manager->getHighScoreModel(Hard);
        ui->tableViewHard->setModel(m_model);
        break;
    }
    m_model->setParent(this);


}
